package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import model.Consorcio;

public class AdmPersistConsorcio {
	
	private static AdmPersistConsorcio instancia;
	
	private AdmPersistConsorcio(){
		
	}
	
	public static AdmPersistConsorcio getInstancia(){
		if(instancia==null)
			instancia=new AdmPersistConsorcio();
		return instancia;
	}
	
	public void insert(Object o){
		
	}
	
	public Consorcio buscarConsorcio(String nombreConsorcio){
		try
		{
			Consorcio a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select numero,nombre,direccion,fondo,cuit from seminario.dbo.Consorcios where nombre = ?");
			s.setString(1, nombreConsorcio);
			ResultSet result = s.executeQuery();			
			
			
			while (result.next())
			{
				int numero = result.getInt(1);
				String nombre = result.getString(2);
				String direccion = result.getString(3);
				float fondo = result.getFloat(4);
				String cuit = result.getString(5);
				
				a = new Consorcio(numero,nombre,direccion,cuit,fondo);
			    //a = new Proveedor(nrocuit,razonSocial);
			    
				
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
			
		}
		catch (Exception e)
		{
			System.out.println("Error al buscar consorcio");
		}
		return null;
	}
	
	
	public Vector<Consorcio> selectAll(){
		try
		{
			Vector<Consorcio>rta = new	Vector<Consorcio>();
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement(" select C.numero,C.nombre,C.direccion,C.fondo,C.cuit from seminario.dbo.Consorcios C ");
			ResultSet result = s.executeQuery();
			while (result.next()){
				
				int numero = result.getInt(1);
				String nombre = result.getString(2);
				String direccion = result.getString(3);
				float fondo = result.getFloat(4);
				String cuit = result.getString(5);
				
				
				Consorcio a = new Consorcio(numero,nombre,direccion,cuit,fondo);
				rta.add(a);
				
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return rta;
			
			
		}catch (Exception e)
		{
			System.out.println("Error al leer la base de datos");
			System.out.println("Stack Trace al escribir la Base de datos: " + e.getStackTrace());
		}
		return null;
	}
}
